﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using cwtrock.model;
using cwtrock.syscommon;
using cwtrock.service.impl;
using cwtrock.util;

namespace cwtrock.view.handle
{
    public partial class Postguide_frm : Form
    {
        private VoucherServiceImpl voucherServiceImpl = new VoucherServiceImpl();
        private static Postguide_frm frm;
        private int page=0;
        private  string info_page_0="";
        private string info_page_1 = "";
        private SysUsr currentUser;
        private Thread thread_post;

        // 带过张的凭证总数
        private int vouchersToBePostedCount;

        // 成功过账的凭证张数
        private int countv;

        #region 单体模式
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static Postguide_frm getInstance()
        {
            if (frm == null)
            {
                frm = new Postguide_frm();

            }

            return frm;
        }
        #endregion

        public Postguide_frm()
        {
            info_page_0 = "本指南将根据本期输入或生成的记账凭证，逐笔登记各总分类账户及明细分类账户，以便查询查询账簿和输出报表。";
            info_page_1 = "按\"完成\"按钮完成过账。";
            InitializeComponent();
            initfrm();

            fwd_btn.Select();

        }

        private void initfrm() {

            vpost_guide_txt.Text = info_page_0;
            back_btn.Enabled = false;
            progressBar1.Visible = false;

            currentUser = AppContextProvider.instance.LogUser;
            
        }

        private void fwd_btn_Click(object sender, EventArgs e)
        {
            fwd_btn.Select();

             page++;
            seeProcessBar();
            fwd_btn.Text = "完成";
            back_btn.Enabled = true;
            if (page == 0) {
                vpost_guide_txt.Text = info_page_0;
               
            } 
            else if (page == 1)
            {
                vpost_guide_txt.Text = info_page_1;
               
            }
            else if (page == 2)
            {
                progressBar1.Select();
                vpost_guide_txt.Text = "";
                fwd_btn.Enabled = false;
                cancel_btn.Enabled = false;
                back_btn.Enabled = false;
                this.ControlBox = false;

                vouchersToBePostedCount = voucherServiceImpl.countVoucherTobePosted();
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;
                progressBar1.Step = 100 / 10;
                thread_post = new Thread(new ThreadStart(Send));
                thread_post.Start();
                progressBar1.BackColor = Color.Green;
 

                //凭证过账
                postVoucher();

            }
            else if (page == 3)
            {
                this.Close();

            }

        }

        // 凭证过账
        private void postVoucher() {

            Vouchers vou = new Vouchers();
            vou.Vposterid = currentUser.Usrid;
            vou.Vposter = currentUser.Usrname;
            vou.Vpoststat =SysConst.VOUCHER_POST_YES;
            vou.Vmakerid = currentUser.Usrid;

            countv = voucherServiceImpl.postVoucher(vou);
          

        }

        private void seeProcessBar() {

            if (page != 2)
            {
                progressBar1.Visible = false;
            }
            else {

                progressBar1.Visible = true;
            }
        
        }

        private void back_btn_Click(object sender, EventArgs e)
        {
            page--;
            fwd_btn.Select();
            if (page == 0)
            {
                vpost_guide_txt.Text = info_page_0;
                back_btn.Enabled = false;
                fwd_btn.Text = "前进 >";
            }
            else if (page == 1)
            {
                vpost_guide_txt.Text = info_page_1;
            }
        }

     
        private void Postguide_frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm = null;
            if (thread_post!=null)
              thread_post.Abort();
        }

        private void cancel_btn_Click(object sender, EventArgs e)
        {
            this.Close();
            frm = null;
            if (thread_post != null)
             thread_post.Abort();
        }

        //模拟进度条
        private void Send()
        {
            int i = 0;

            while (i <= vouchersToBePostedCount)
            {
                //显示进度信息
                int process = 0;
                if (vouchersToBePostedCount != 0) {

                    process = (i*100 )/ vouchersToBePostedCount;
                }
                    
                    
                this.ShowPro(process);
                i++; //模拟发送多少

                Thread.Sleep(200);
            }
            string _ok = finishPosting(posted);


            Thread.CurrentThread.Abort();


        }
        private delegate void ProgressBarShow(int i);
        private void ShowPro(int value)
        {
          //  if (value != 100)
          //  {
                if (this.InvokeRequired)
                {
                    this.Invoke(new ProgressBarShow(ShowPro), value);
                }
                else
                {
                    this.progressBar1.Value = value;
                    this.vpost_guide_txt.Text = "已完成" + value + "%";
                }
         //   }
            //else
            //{

            //    string _ok = finishPosting(posted);
            //}
        }

        //建立个委托
        private delegate string delegatePost();

        //搞个最简单滴取值滴方法~
        private string posted()
        {
            this.vpost_guide_txt.Text = "过账完成！";
            post_result_txt.Text = "成功过账" + countv + "张凭证！";
            this.progressBar1.Visible = false;
            this.fwd_btn.Enabled = true;
            fwd_btn.Select();
            return "OK";
        }

        //判断一下是不是该用Invoke滴~，不是就直接返回~
        private string finishPosting(delegatePost postfunc)
        {
            if (this.InvokeRequired)
            {
                return (string)this.Invoke(postfunc);
            }
            else
            {
                return postfunc();
            }
        }

    }
}
